﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileTest
{
    public partial class Form1 : Form
    {   
        //设定两个文件路径
        private string filePath1 = string.Empty;
        private string filePath2 = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取选择的文件路径
                    filePath1 = openFileDialog.FileName;
                    textBox1.Text = filePath1;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                   
                    filePath2 = openFileDialog.FileName;
                    textBox2.Text = filePath2;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //判断是否有两个文件
            if (!string.IsNullOrEmpty(filePath1)&&!string.IsNullOrEmpty(filePath2))
            {
                try
                {                 
                    //获取合并后文件路径
                    string currentPath=Environment.CurrentDirectory+"\\Data\\";
                    string mergedName = "mergedFile.txt";
                    string targetPath=currentPath+mergedName;
                    using (StreamReader reader1 = new StreamReader(filePath1))
                    using (StreamReader reader2 = new StreamReader(filePath2))
                    using (StreamWriter writer = new StreamWriter(targetPath))
                    {
                        // 读取第一个文件的内容并写入目标文件  
                        string line;
                        while ((line = reader1.ReadLine()) != null)
                        {
                            writer.WriteLine(line);
                        }

                        // 读取第二个文件的内容并追加到目标文件  
                        while ((line = reader2.ReadLine()) != null)
                        {
                            writer.WriteLine(line);
                        }
                    }

                    //执行到底，合并成功
                    label3.Text = "文件合并成功";
                }
                catch(Exception ex)
                { 
                    label3.Text = ex.Message;
                }
            }
            else
            {
                label3.Text = "有文件为空，请确保您已存入两个文件";
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
